<?php
error_reporting(E_ALL ^ E_NOTICE);
 //FUNCTION: Connect to PostgreSql database
 
 //create a connection to postgresql database
set_time_limit(0);
ini_set("memory_limit", "200M");
  pg_connect("host=localhost dbname=comds  user=root  password=root")
   or die('Could not connect:'.pg_last_error());
   
 /* //query data from postgresql database
 $query  = 'SELECT * FROM comds ';
 $result = pg_query($query) or die('Query failed:'.pg_last_error());
 
 //fetch data from postgresql database
 echo "<table border='1'>";
 while($row = pg_fetch_array($result,null,PGSQL_ASSOC)){
  echo "<tr>";
  foreach($row as $value){
   echo "<td style='color:blue'>".$value."</td>";
  }
  echo "</tr>";
 }
 echo "</table>";
 
 //release query result
 pg_free_result($result);
 
 //close  connection to postgresql database
 pg_close($dbconn);

*/

//error_reporting(E_ERROR | E_WARNING | E_PARSE);
////mysql_connect(localhost, root, root);
//@mysql_select_db(comds) or die("Unable to select database");

//$dateTime = date("Y-m-d",strtotime("-1 day"));
//$handle = fopen("http://192.168.0.185/log/test/$dateTime.'a'", "r");
$handle = fopen("http://192.168.0.17/log/comds/log.txt", "r");
while (!feof($handle)) {
    $buffer = fgets($handle, 10960);
    // $array[] = $buffer;
    $arr = json_decode($buffer, true);
    $id100 = $arr["data"];
    $arr2 = json_decode($id100, true);


    $id = $arr2["TotalExp"];
    $id2 = $arr2["FirstLoginTime"];
    $id3 = $arr2["TotalDollorGet"];
    $id4 = $arr2["TodayGameTime"];
    $id5 = $arr2["TodayTCrystalLose"];
    $id6 = $arr2["Crystal"];
    $id7 = $arr2["TodayGoldGet"];
    $id8 = $arr2["TodayGoldLose"];
    $id9 = $arr2["Cracked"];
    $id10 = $arr2["WeaponList"];

    $id11 = $arr2["TodayExp"];  //2
    $id12 = $arr2["TodayIapInfo"];
    $id13 = $arr2["Gold"];
    $id14 = $arr2["Level"];
    $id15 = $arr2["GameActiveTime"]; //3
    $id16 = $arr2["TotalGoldGet"];
    $id17 = $arr2["UILastId"];
    $id18 = $arr2["HireoutSelfTimes"];
    $id19 = $arr2["TotalGameTime"]; //4
    $id20 = $arr2["TodayTCrystalGet"];


    $id21 = $arr["deviceid"];
    $id22 = $arr["gamename"];
   // $id23 = $arr["whatday"];
    $id24 = $arr["createdate"];


    $s = 0;
    foreach ((array) $arr as $item => $value) {   //Warning: Invalid argument supplied for foreach
        $s = count($value); //
    }
    for ($i = 0; $i < $s; $i++) {
    	
        $TotalExp = $id;
        $FirstLoginTime = $id2;
        $TotalDollorGet = $id3;
        $TodayGameTime = $id4;
        $TodayTCrystalLose = $id5;
        $Crystal = $id6;
        $TodayGoldGet = $id7;
        $TodayGoldLose = $id8;
        $Cracked = $id9;
        $WeaponList = $id10;
        $TodayExp = $id11;
        $TodayIapInfo = $id12;
        $Gold = $id13;
        $Level = $id14;
        $GameActiveTime = $id15;
        $TotalGoldGet = $id16;
        $UILastId = $id17;
        $HireoutSelfTimes = $id18;
        $TotalGameTime = $id19;
        $TodayTCrystalGet = $id20;
        $deviceid = $id21;
        $gamename = $id22;
       // $whatday = $id23;
        $createdate = $id24;
        $date = substr($id24,0,10);
//INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');

   //var_dump($TotalExp);
 //("INSERT INTO comds (totalexp, firstlogintime,totaldollorget,todaygametime,todaytcrystallose) 
    //                VALUES('$TotalExp','$FirstLoginTime','$TotalDollorGet','$TodayGameTime','$TodayTCrystalLose')") or die(pg_last_error());

        pg_query("INSERT INTO comds (totalexp, firstlogintime,totaldollorget,todaygametime,todaytcrystallose,
                                        crystal,todaygoldget,todaygoldlose,cracked,weaponlist,todayexp,todayiapinfo,
                                        gold,level,gameactivetime,totalgoldget,uilastid,hireoutselftimes,totalgametime,
                                        todaytcrystalget,deviceid,gamename,createdate,date,whatday) 
                    VALUES('$TotalExp','$FirstLoginTime','$TotalDollorGet','$TodayGameTime','$TodayTCrystalLose','$Crystal','$TodayGoldGet','$TodayGoldLose','$Cracked','$WeaponList','$TodayExp',
                           '$TodayIapInfo','$Gold','$Level','$GameActiveTime','$TotalGoldGet','$UILastId','$HireoutSelfTimes','$TotalGameTime','$TodayTCrystalGet',
                           '$deviceid','$gamename','$createdate','$date','$FirstLoginTime')") or die(pg_last_error());
    }
}
fclose($handle);
//fclose($fp);
print "Import successful!<br>";

?> 
